package com.clz.appdesign.appdependence.net.download;

import com.clz.appdesign.appdependence.net.APIFunction;
import com.clz.appdesign.appdependence.net.RetrofitFactory;
import com.clz.appdesign.appdependence.net.config.HttpConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author chenliangzhi
 * @date 2018/3/28
 * @describe
 */

public class RetrofitDownLoad {
    private static RetrofitDownLoad mRetrofitDownload;
    private static APIFunction mAPIFunction;

    private RetrofitDownLoad() {
        OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
                .readTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
                .writeTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)
                //添加日志拦截器
                .addInterceptor(new ProgressInterceptor())
                .retryOnConnectionFailure(true)
                .build();
        Retrofit mRetrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(mOkHttpClient)
                .build();
        mAPIFunction = mRetrofit.create(APIFunction.class);

    }

    public static RetrofitDownLoad getInstence() {
        if (mRetrofitDownload == null) {
            synchronized (RetrofitFactory.class) {
                if (mRetrofitDownload == null) {
                    mRetrofitDownload = new RetrofitDownLoad();
                }
            }

        }
        return mRetrofitDownload;
    }

    public APIFunction API() {
        return mAPIFunction;
    }
}
